import { login, logout, getInfo, emailLogin } from '~/api/user'
import { getToken, setToken, removeToken } from '~/utils/auth'

const getDefaultState = () => {
    return {
        loginIsShow: false,
        name: '',
        avatar: '',
        userid: undefined,
        token: "",
    }
}

const state = getDefaultState()

const actions = {
    updateLoginStatus({ commit }, value) {
        commit('UPDATE_LOGIN_STATUS', value)
    },
    setUserInfo({ commit }, value) {
        commit('SET_USERINFO', value)
    },
    setToke({ commit }, value) {
        commit('SET_TOKEN', value)
    },
    
    // 账号密码登入
    login({ commit }, userInfo) {
        const { account, password, verifyCode, uuid } = userInfo
        return new Promise((resolve, reject) => {
            login({ account: account.trim(), password: password, verifyCode, uuid }).then(response => {
                const { data } = response
                commit('SET_TOKEN', data.token)
                setToken(data.token)
                resolve(data)
            }).catch(error => {
                reject(error)
            })
        })
    },

    // 邮箱登入
    emailLogin({ commit }, userInfo) {
        const { email, verifyCode, uuid } = userInfo
        return new Promise((resolve, reject) => {
            emailLogin({ email: email.trim(), verifyCode, uuid }).then(response => {
                const { data } = response
                commit('SET_TOKEN', data.token)
                setToken(data.token)
                resolve(data)
            }).catch(error => {
                reject(error)
            })
        })
    },

    // 获取用户信息
    getInfo({ commit, state }) {
        return new Promise((resolve, reject) => {
            getInfo(state.token).then(response => {
                const { data } = response
                if (!data) {
                    return reject('Verification failed, please Login again.')
                }
                commit('SET_USERID', data.userid)
                commit('SET_NAME', data.username)
                commit('SET_AVATAR', data.blogUser.avatar)
                resolve(data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    // 退出登入
    logout({ commit, state }) {
        return new Promise((resolve, reject) => {
            logout(state.token).then(() => {
                removeToken()
                commit('RESET_STATE')
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },
    resetToken({ commit }) {
        return new Promise(resolve => {
            removeToken()
            commit('RESET_STATE')
            resolve()
        })
    }

}

const mutations = {
    RESET_STATE: (state) => {
        Object.assign(state, getDefaultState())
    },
    UPDATE_LOGIN_STATUS(state, value) {
        state.loginIsShow = value
    },
    SET_NAME: (state, name) => {
        state.name = name
    },
    SET_AVATAR: (state, avatar) => {
        state.avatar = avatar
    },
    SET_USERID: (state, userid) => {
        state.userid = userid
    },
    SET_TOKEN(state, value){
        state.token = value
    }

}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
